package com.ald.crossex.action.wechat;

import com.ald.crossex.dto.wechat.BaseResponse;
import com.ald.crossex.dto.wechat.SubmitFeedBackRequest;
import com.ald.crossex.entity.TCeCoreUser;
import com.ald.crossex.entity.TCeCoreWechat;
import com.ald.crossex.service.UserService;
import com.ald.crossex.service.wechat.WechatService;
import com.ald.crossex.system.SystemCache;
import com.ald.crossex.system.SystemConfig;
import org.marker.weixin.MySecurity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lishijiong on 15/2/11.
 */
@Controller
@RequestMapping(value = "/")
public class WeChatController extends BaseController{

    @Autowired
    private WechatService wechatService;
    @Autowired
    private UserService userService;


    @RequestMapping(value = "feedback")
    public ModelAndView feedBackRedirect(@RequestParam(value = "code") String code, @RequestParam(value = "state") String state, HttpServletRequest request) {

        TCeCoreWechat wechat = webAuthorize(wechatService, code, state);
        ModelAndView mav = new ModelAndView("feedback");
        if(wechat!=null){
            mav.addObject("wid",wechat.getWid());
        }
        TCeCoreUser user = userService.getUserByWechatId(wechat.getWid());
        if(user != null){
            mav.addObject("uid",user.getUid());
        }
        generateJsSDKConfig(request, mav, code, state);
        return mav;
    }
    /**
    @RequestMapping(value = "feedback")
    public String feedBackRedirect(){
        return "feedback";
    }**/
    @RequestMapping(value = "feedback/submit")
    @ResponseBody
    public BaseResponse submitFeedBack(@RequestBody SubmitFeedBackRequest request){

        wechatService.submitFeedback(request);
        return new BaseResponse();
    }

    @RequestMapping(value = "jssdk")
    public ModelAndView jsSDKTest(@RequestParam(value = "code") String code, @RequestParam(value = "state") String state, HttpServletRequest request){
        ModelAndView mav = new ModelAndView("jssdk");
        long timestamp = System.currentTimeMillis();
        String url = request.getRequestURL().toString() + "?code=" + code + "&state=" + state;
        String noncestr = SystemConfig.getVal("wechat_mp_nonceStr");
        String jsapi_ticket = (String) SystemCache.get("jsapi_ticket").getObject();
        String signature = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + noncestr + "&timestamp=" + timestamp + "&url=" + url;
        MySecurity security = new MySecurity();
        logger.info("before Security encode " + signature);
        signature = security.encode(signature, MySecurity.SHA_1);
        logger.info("after Security encode " + signature);
        mav.addObject("appId", SystemConfig.getVal("wechat_mp_AppID"));
        mav.addObject("timestamp", timestamp);
        mav.addObject("nonceStr", noncestr);
        mav.addObject("signature", signature);
        return mav;
    }

}
